#include "modern_chat_app.h"
#include <wx/sizer.h>
#include <wx/statline.h>

wxBEGIN_EVENT_TABLE(ModernChatFrame, wxFrame)
    EVT_BUTTON(wxID_ANY, ModernChatFrame::OnSendMessage)
    EVT_CHOICE(wxID_ANY, ModernChatFrame::OnAgentChanged)
    EVT_BUTTON(wxID_CLEAR, ModernChatFrame::OnClearHistory)
wxEND_EVENT_TABLE()

ModernChatFrame::ModernChatFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
    : wxFrame(NULL, wxID_ANY, title, pos, size) {
    
    // 设置图标和样式
    this->SetMinSize(wxSize(800, 600));
    
    // 初始化智能体
    agents.push_back(std::make_unique<SmartChatAgent>());
    agents.push_back(std::make_unique<CodeGeneratorAgent>());
    agents.push_back(std::make_unique<CodeAnalysisAgent>());
    
    CreateControls();
    SetupEventHandlers();
    
    // 设置默认智能体
    if (agentChoice && agentChoice->GetCount() > 0) {
        agentChoice->SetSelection(0);
    }
}

void ModernChatFrame::CreateControls() {
    // 创建主分割窗口
    splitter = new wxSplitterWindow(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxSP_3D);
    splitter->SetSashGravity(0.2); // 左侧占20%
    
    // 左侧面板 - 设置和历史
    leftPanel = new wxPanel(splitter, wxID_ANY);
    wxBoxSizer* leftSizer = new wxBoxSizer(wxVERTICAL);
    
    // 智能体选择
    wxStaticText* agentLabel = new wxStaticText(leftPanel, wxID_ANY, wxT("选择AI智能体:"));
    leftSizer->Add(agentLabel, 0, wxALL, 5);
    
    agentChoice = new wxChoice(leftPanel, wxID_ANY);
    for (const auto& agent : agents) {
        agentChoice->Append(agent->GetName());
    }
    leftSizer->Add(agentChoice, 0, wxALL | wxEXPAND, 5);
    
    // 智能体描述
    wxStaticText* descLabel = new wxStaticText(leftPanel, wxID_ANY, wxT("智能体描述:"));
    leftSizer->Add(descLabel, 0, wxALL, 5);
    
    wxTextCtrl* descText = new wxTextCtrl(leftPanel, wxID_ANY, wxT(""), 
                                         wxDefaultPosition, wxDefaultSize, 
                                         wxTE_MULTILINE | wxTE_READONLY);
    descText->SetMinSize(wxSize(-1, 80));
    leftSizer->Add(descText, 0, wxALL | wxEXPAND, 5);
    
    // 分隔线
    leftSizer->Add(new wxStaticLine(leftPanel, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLI_HORIZONTAL), 
                   0, wxEXPAND | wxALL, 5);
    
    // 对话历史
    wxStaticText* historyLabel = new wxStaticText(leftPanel, wxID_ANY, wxT("对话历史:"));
    leftSizer->Add(historyLabel, 0, wxALL, 5);
    
    historyList = new wxListBox(leftPanel, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0, NULL, wxLB_SINGLE);
    leftSizer->Add(historyList, 1, wxALL | wxEXPAND, 5);
    
    // 清除按钮
    clearButton = new wxButton(leftPanel, wxID_CLEAR, wxT("清除历史"));
    leftSizer->Add(clearButton, 0, wxALL | wxEXPAND, 5);
    
    leftPanel->SetSizer(leftSizer);
    
    // 右侧面板 - 聊天界面
    rightPanel = new wxPanel(splitter, wxID_ANY);
    wxBoxSizer* rightSizer = new wxBoxSizer(wxVERTICAL);
    
    // 聊天显示区域
    chatDisplay = new wxTextCtrl(rightPanel, wxID_ANY, wxT(""), 
                                wxDefaultPosition, wxDefaultSize, 
                                wxTE_MULTILINE | wxTE_READONLY | wxTE_RICH2);
    chatDisplay->SetMinSize(wxSize(-1, 400));
    chatDisplay->SetBackgroundColour(wxColour(240, 240, 240));
    rightSizer->Add(chatDisplay, 1, wxALL | wxEXPAND, 5);
    
    // 输入区域
    wxBoxSizer* inputSizer = new wxBoxSizer(wxHORIZONTAL);
    
    inputText = new wxTextCtrl(rightPanel, wxID_ANY, wxT(""), 
                              wxDefaultPosition, wxDefaultSize, 
                              wxTE_PROCESS_ENTER);
    inputText->SetHint(wxT("输入你的问题或指令..."));
    inputSizer->Add(inputText, 1, wxALL | wxEXPAND, 5);
    
    sendButton = new wxButton(rightPanel, wxID_ANY, wxT("发送"));
    inputSizer->Add(sendButton, 0, wxALL, 5);
    
    rightSizer->Add(inputSizer, 0, wxEXPAND);
    
    rightPanel->SetSizer(rightSizer);
    
    // 设置分割窗口
    splitter->SplitVertically(leftPanel, rightPanel);
    splitter->SetMinimumPaneSize(200); // 最小宽度
    
    // 设置主窗口布局
    wxBoxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);
    mainSizer->Add(splitter, 1, wxEXPAND);
    this->SetSizer(mainSizer);
    
    // 更新描述
    if (agentChoice->GetCount() > 0) {
        int selection = agentChoice->GetSelection();
        if (selection != wxNOT_FOUND) {
            descText->SetValue(agents[selection]->GetDescription());
        }
    }
}

void ModernChatFrame::SetupEventHandlers() {
    // 输入框回车事件
    inputText->Bind(wxEVT_TEXT_ENTER, &ModernChatFrame::OnSendMessage, this);
}

void ModernChatFrame::OnSendMessage(wxCommandEvent& event) {
    wxString message = inputText->GetValue().Trim();
    if (message.IsEmpty()) {
        return;
    }
    
    // 获取当前选择的智能体
    int agentIndex = agentChoice->GetSelection();
    if (agentIndex == wxNOT_FOUND) {
        chatDisplay->AppendText(wxT("\n❌ 请先选择一个AI智能体！\n"));
        return;
    }
    
    // 添加用户消息到显示
    AddMessageToHistory(wxT("👤 用户"), message);
    
    // 清空输入框
    inputText->Clear();
    
    // 处理消息并显示响应
    wxString response = agents[agentIndex]->ProcessMessage(message);
    AddMessageToHistory(wxT("🤖 AI"), response);
    
    // 添加到历史列表
    wxString shortMessage = message;
    if (shortMessage.Length() > 30) {
        shortMessage = shortMessage.Left(27) + wxT("...");
    }
    historyList->Append(wxString::Format(wxT("[%s] %s"), 
        agents[agentIndex]->GetName(), shortMessage));
}

void ModernChatFrame::OnAgentChanged(wxCommandEvent& event) {
    // 这里可以更新智能体描述等
    wxString welcomeMsg = wxString::Format(wxT("\n=== 切换到 %s ===\n%s\n\n"), 
        agents[event.GetSelection()]->GetName(),
        agents[event.GetSelection()]->GetDescription());
    chatDisplay->AppendText(welcomeMsg);
}

void ModernChatFrame::OnClearHistory(wxCommandEvent& event) {
    chatDisplay->Clear();
    historyList->Clear();
    chatDisplay->AppendText(wxT("🗑️ 对话历史已清除\n\n"));
}

void ModernChatFrame::AddMessageToHistory(const wxString& role, const wxString& content) {
    wxString formattedMessage = wxString::Format(wxT("\n%s:\n%s\n"), role, content);
    chatDisplay->AppendText(formattedMessage);
    
    // 自动滚动到底部
    chatDisplay->ShowPosition(chatDisplay->GetLastPosition());
}
